﻿#region << 文 件 说 明 >>
/*----------------------------------------------------------------
// 文件名称：LoginViewModel
// 创 建 者：杨程
// 创建时间：2021/9/13 16:50:42
// 文件版本：V1.0.0
// ===============================================================
// 功能描述：
//		
//
//----------------------------------------------------------------*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Vampirewal.Core;
using Vampirewal.Core.IoC;
using Vampirewal.Core.Interface;
using Vampirewal.Core.Models;
using Vampirewal.Core.SimpleMVVM;

namespace V.CodeGenerator.Test.ViewModel
{
    [VampirewalIoCRegister("LoginViewModel", RegisterType.ViewModel)]
    public class LoginViewModel : ViewModelBase
    {
        
        

        public LoginViewModel(IDataContext _DC, IAppConfig appConfig):base(_DC, appConfig)
        {
            //构造函数
            //appConfig.LoadAppConfig();
        }

        /// <summary>
        /// 初始化数据，优先于当前构造函数
        /// </summary>
        public override void InitData()
        {
            //Config.LoadAppConfig();

            Title = "登陆";
        }

        /// <summary>
        /// 注册消息，优先于当前构造函数
        /// </summary>
        public override void MessengerRegister()
        {
            
        }

        /// <summary>
        /// 接收传递过来的参数，优先于当前构造函数
        /// </summary>
        /// <param name="obj"></param>
        public override void PassData(object obj)
        {
            
        }

        /// <summary>
        /// 当前窗体如果是dialogwindow方式打开，这个可以返回数据
        /// </summary>
        /// <returns></returns>
        public override object GetResult()
        {
            return base.GetResult();
        }

        

        #region 属性
        public string UserName { get; set; }

        public string PassWord { get; set; }
        #endregion

        #region 公共方法

        #endregion

        #region 私有方法

        #endregion

        #region 命令
        public RelayCommand<Window> WindowsLoadCommand => new RelayCommand<Window>((w) =>
          {
              this.View = w; 
          });

        public RelayCommand LoginInCommand => new RelayCommand(() =>
        {
            if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(PassWord))
            {
                HandyControl.Controls.MessageBox.Show("请输入用户名或密码！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            var LoginUser= DC.Client.Queryable<FrameworkUserBase>()
                           //.Include(x=>x.UserRoles)
                           .Where(f => f.ITCode == UserName && f.Password == DESHelper.Encrypt3Des(PassWord, "Test"))
                           .First();

            if (LoginUser!=null)
            {
                var _loginUserInfo = new LoginUserInfo()
                {
                    Id=Guid.NewGuid(),
                    ITCode= LoginUser.ITCode,
                    Name= LoginUser.Name,
                    Roles= LoginUser.UserRoles.Select(s=>new FrameworkRole() { RoleCode=s.RoleCode}).ToList()
                };

                SystemDataContext.GetInstance().loginUserInfo = _loginUserInfo;

                HandyControl.Controls.MessageBox.Show("登陆成功！", "恭喜", MessageBoxButton.OK, MessageBoxImage.Information);
                Messenger.Default.Send("OpenWinodw", "MainView");

                ((Window)View).Close();
            }
        });
        #endregion
    }
}
